using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

namespace ThaiLanguageTools
{
    public class ChangeRuleList 
    {
        [XmlElement("Rule")]
        public List<ChangeRule> Rules = new List<ChangeRule>();

        public ChangeRuleList() { }

        public static ChangeRuleList Load(String file)
        {
            if (Path.GetExtension(file).ToLower() == ".xml")
            {
                return Utils.LoadXml<ChangeRuleList>(file);                 
            }

            // Easier format
            ChangeRuleList rv = new ChangeRuleList();
            using (StreamReader sr = new StreamReader(file))
            {
                while (true)
                {
                    String line = sr.ReadLine();
                    if (line == null) { break; }
                    ChangeRule rule = ChangeRule.Parse(line);
                    if (rule == null) { continue; }
                    rv.Rules.Add(rule);
                }
            }
            return rv;
        }

        public void Save(String file)
        {
            if (Path.GetExtension(file).ToLower() == ".xml")
            {
                Utils.SaveXml<ChangeRuleList>(file, this);
                return;
            }

            using (StreamWriter sw = new StreamWriter(file))
            {
                foreach (ChangeRule r in Rules)
                {
                    sw.WriteLine(r.ToStringRep());
                }
            }
        }
    }
}
